added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / VBASPNETAJAXWebChat / Services / Transition.svc.vb
blob269e64aef0dc7b033dc6ba0a9a0136f643f9534b
1 '***************************** Module Header ******************************\
2 '* Module Name: RoomTalker.svc.vb
3 '* Project: VBASPNETAJAXWebChat
4 '* Copyright (c) Microsoft Corporation
5 '*
6 '* The project illustrates how to design a simple AJAX web chat application.
7 '* We use jQuery, ASP.NET AJAX at client side and Linq to SQL at server side.
8 '* In this sample, we could create a chat room and invite someone
9 '* else to join in the room and start to chat.
10 '*
11 '* In this file, we create an Ajax-enabled WCF service which used to be called
12 '* from the client side.
13 '*
14 '* This source is subject to the Microsoft Public License.
15 '* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
16 '* All other rights reserved.
18 '\****************************************************************************
20 Imports System.ServiceModel
21 Imports System.ServiceModel.Activation
22 Imports System.ServiceModel.Web
24 <ServiceContract([Namespace]:="http://VBASPNETAJAXWebChat",
25 SessionMode:=SessionMode.Allowed)> _
26 <AspNetCompatibilityRequirements(
27 RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
28 Public Class Transition
30 <OperationContract()> _
31 Public Sub CreateChatRoom(ByVal useralias As String,
32 ByVal roomName As String,
33 ByVal password As String,
34 ByVal maxUser As Integer,
35 ByVal needPassword As Boolean)
37 If maxUser < 2 Then
38 maxUser = 2
39 End If
41 Dim roomid As Guid = ChatManager.CreateChatRoom(roomName,
42 password,
43 False,
44 maxUser,
45 needPassword)
47 End Sub
49 <OperationContract()> _
50 Public Function JoinChatRoom(ByVal roomid As String,
51 ByVal [alias] As String) As ChatRoom
52 Dim rid As Guid
53 If Guid.TryParse(roomid, rid) Then
54 ChatManager.JoinChatRoom(rid, HttpContext.Current, [alias])
56 Return New ChatRoom(rid)
57 Else
58 Return Nothing
59 End If
61 End Function
63 <OperationContract()> _
64 Public Sub LeaveChatRoom(ByVal roomid As String)
65 If roomid Is Nothing Then
66 roomid = GetGUIDFromQuery(
67 HttpContext.Current.Request.UrlReferrer.Query).ToString()
68 End If
69 Dim rid As Guid
70 If Guid.TryParse(roomid, rid) Then
71 ChatManager.LeaveChatRoom(rid, HttpContext.Current)
72 Else
73 Return
74 End If
75 End Sub
77 <OperationContract()> _
78 Public Function GetChatRoomList() As List(Of ChatRoom)
79 Dim list As List(Of tblChatRoom) = ChatManager.GetChatRoomList()
80 Dim result As New List(Of ChatRoom)()
81 For Each room As tblChatRoom In list
82 result.Add(New ChatRoom(room.ChatRoomID))
83 Next
84 Return result
85 End Function
87 <OperationContract()> _
88 Public Function GetChatRoomInfo(ByVal RoomID As String) As ChatRoom
89 Dim rim As Guid
90 If Guid.TryParse(RoomID, rim) Then
91 Return New ChatRoom(rim)
92 Else
93 Return Nothing
94 End If
96 End Function
98 <OperationContract()> _
99 Public Function GetRoomTalkerList() As List(Of RoomTalker)
101 Dim result As New List(Of RoomTalker)()
102 Dim roomid As Guid = GetGUIDFromQuery(
103 HttpContext.Current.Request.UrlReferrer.Query)
104 If roomid <> Guid.Empty Then
105 Dim talkerList As List(Of tblTalker) =
106 ChatManager.GetRoomTalkerList(roomid)
107 For Each talker As tblTalker In talkerList
108 result.Add(New RoomTalker(talker, HttpContext.Current))
109 Next
110 End If
111 Return result
113 End Function
115 <OperationContract()> _
116 Public Function SendMessage(ByVal message As String) As Boolean
117 Dim roomid As Guid = GetGUIDFromQuery(
118 HttpContext.Current.Request.UrlReferrer.Query)
120 If roomid <> Guid.Empty Then
121 Dim talker As tblTalker =
122 ChatManager.FindTalker(roomid, HttpContext.Current)
123 ChatManager.SendMessage(talker, message)
124 Return True
125 Else
127 Return False
128 End If
129 End Function
131 <OperationContract()> _
132 Public Function RecieveMessage() As List(Of Message)
133 Dim result As New List(Of Message)()
134 Dim roomid As Guid = GetGUIDFromQuery(
135 HttpContext.Current.Request.UrlReferrer.Query)
137 If roomid <> Guid.Empty Then
138 Dim messageList As List(Of tblMessagePool) =
139 ChatManager.RecieveMessage(ChatManager.GetChatRoom(roomid))
141 For Each msg As tblMessagePool In messageList
142 result.Add(New Message(msg, HttpContext.Current))
143 Next
144 End If
145 Return result
147 End Function
149 Private Function GetGUIDFromQuery(ByVal query As String) As Guid
150 Dim rim As Guid
151 If String.IsNullOrEmpty(query) Then
152 Return Guid.Empty
153 End If
155 Dim reg As New Regex(
156 "i=([0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12})")
157 Dim gid As String = reg.Match(query).Groups(1).Value
158 If Guid.TryParse(gid, rim) Then
159 Return rim
160 Else
161 Return Guid.Empty
162 End If
164 End Function
165 End Class